<?php
/**
 * Created by PhpStorm.
 * User: cyc
 * Date: 2017/11/2
 * Time: 下午4:45
 */

class MsggGlobalReader implements ConfigReader
{

    private $status;

    public function read($status = MsggConfig::STATUS_OPEN, $first = true)
    {
        $this->status = $status;
        $configs = MsggConfig::model()->findAllByAttributes(['type' => MsggGlobalConfig::CONFIG_TYPE, 'status' => $status]);
        $configs = array_map(function ($config) {
            return new MsggGlobalConfig($config);
        }, $configs);

        if ($first && sizeof($configs) > 0) {
            $configs = $configs[0];
        }
        return $configs;
    }

    public function searchByKey(MsggConfig $config)
    {
        $criteria = new CDbCriteria;
        $criteria->addColumnCondition(['type' => MsggGlobalConfig::CONFIG_TYPE]);
        if (!empty($config->status)) {
            $criteria->addColumnCondition(array('status' => $config->status));
        }
        if ($config->searchKey !== '') {
            $criteria->addSearchCondition('config', $config->searchKey, true, 'OR');
            $criteria->addSearchCondition('name', $config->searchKey, true, 'OR');
            $criteria->addSearchCondition('status', $config->searchKey, true, 'OR');
        }
        return new CActiveDataProvider($config, array(
            'criteria' => $criteria,
        ));
    }


}